package MediaOnline.WebService;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import MediaOnline.Adapter.DanhSachTapPhimAdapter;
import MediaOnline.Data.ActorData;
import MediaOnline.Data.ListFilmForMedia;
import MediaOnline.Data.filmDetail;
import MediaOnline.Data.videoData;
import MediaOnline.GUI.Nahi_MediaActivity;
import MediaOnline.GUI.actFilmDetailInfo2;
import MediaOnline.General.downloadImage;
import MediaOnline.General.paramConst;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

public class GetFilmByID {
/** Called when the activity is first created. */
    
	private static final String Soap_MethodName = "AndroidGetSubFilmByFilmID";
	private static final String Soap_Action = paramConst.Webservice_NameSpace+Soap_MethodName;
	private static final String Soap_NameSpace = paramConst.Webservice_NameSpace;
	private static final String Soap_URL = paramConst.Webservice_Url+"FilmService.asmx?op="+Soap_MethodName;
	public Context mContext;
	public ArrayList<String> arrUser;
	public SoapObject request, request2;
	public SoapSerializationEnvelope Soap_Evelope;
	public ArrayAdapter<String> adpUser;
	
	public ArrayList<String> arrActorName = new ArrayList<String>();
	public ArrayList<String> arrActorID = new ArrayList<String>();
	public ArrayList<String> arrActorPicture = new ArrayList<String>();
	
	
	
	public boolean isOnline=false;
	//ProgressDialog progress; 
		public GetFilmByID(Context ctx){
			mContext = ctx;
			request = new SoapObject(Soap_NameSpace, Soap_MethodName);
			Soap_Evelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        Soap_Evelope.dotNet=true;
	        request.addProperty("FilmID",Integer.parseInt(filmDetail.FilmID));
	        request.addProperty("Key",videoData.MAIN_KEY);
	        Soap_Evelope.setOutputSoapObject(request);
	        //progress = new ProgressDialog(ctx);
	        ListFilmForMedia.lstFilmID.clear();
			ListFilmForMedia.lstFilmUrl.clear();
			ListFilmForMedia.lstTap.clear();
		}
		
		public void get(){
			arrActorName = new ArrayList<String>();
			arrActorID = new ArrayList<String>();
			arrActorPicture = new ArrayList<String>();
			getFilmByID c = new getFilmByID();
	      	c.execute();
        }

	class getFilmByID extends AsyncTask<String, Void, String> {

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			//Luu thong tin danh sach actor
			ActorData.lstActorID = arrActorID;
			ActorData.lstActorName = arrActorName;
			ActorData.lstActorUrl = arrActorPicture;
			DanhSachTapPhimAdapter.arrTapFilm = ListFilmForMedia.lstTap;
			//Start activity film info
			actFilmDetailInfo2.UpdateInfo();
			//progress.dismiss();
			Log.d("Asynctask ::: ", "Post");			
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			Log.d("Asynctask ::: ", "Pre");
			//Chay progress dialog
			/*progress.setMessage("Dang tai du lieu. Vui long doi!");
			progress.setIndeterminate(true);
			progress.setCancelable(false);
			progress.show();*/
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			
		}


		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			Log.d("Asynctask ::: ", "Background");
	        HttpTransportSE androidHttpTransport = new HttpTransportSE(Soap_URL);
	        
	     	//CallService
	      	try{
	      		androidHttpTransport.call(Soap_Action, Soap_Evelope);
	            //Lay du lieu file xml
	      		SoapObject requestall=(SoapObject) Soap_Evelope.getResponse();
	      		Log.d("request all::::", requestall.toString());
	            //loc cac thong tin thua
	      		
	            //request lay thong tin bo phim
	            request = (SoapObject) requestall.getProperty(0);
	            int i = request.getPropertyCount();
	            Log.d("So dong du lieu :::::::", ""+i+" dong");
	            int x=0;
	            //Lay thong tin bo phim
		        request2=(SoapObject) request.getProperty(0);
		        filmDetail.FilmNameChinh=request2.getProperty("FilmName").toString();
		        ListFilmForMedia.FilmName=filmDetail.FilmNameChinh;
		        filmDetail.FilmNamePhu = request2.getProperty("FilmNameOriginal").toString();
		        filmDetail.Country=request2.getProperty("Country").toString();
		        filmDetail.Language=request2.getProperty("Language").toString();
		        filmDetail.Duration=request2.getProperty("Time").toString();
		        filmDetail.Description=request2.getProperty("Desciption").toString();
		        filmDetail.Sound=request2.getProperty("Sound").toString();
		        filmDetail.Rate=request2.getProperty("RatingSum").toString();
		        filmDetail.Price=request2.getProperty("Price").toString();
		        String imgurl=paramConst.URL_ROOT+request2.getProperty("FullImage").toString();
		        downloadImage d = new downloadImage();
		        filmDetail.FilmImgUrl = d.downloadDrawable(imgurl);
		        
		        //Lay thong tin cac tap phim
		        request = (SoapObject) requestall.getProperty(0);
		        Log.d("Thong tin cac tap phim1::::", request.toString());
		        request=(SoapObject) request.getProperty(1);
		        int SoTap = request.getPropertyCount();
		        Log.d("Thong tin tung tap phim1::::", request.toString());
		        Log.d("So tap phim:::::::", ""+SoTap);
		        if (SoTap>1){
		        	//Clear du lieu
		        	for (int j = 0; j < SoTap; j++) {
			        	request2=(SoapObject) request.getProperty(j);
			        	//link tung tap
			        	String link = request2.getAttributeAsString(0);
			        	//Order
			        	int Tap = Integer.parseInt(request2.getAttributeAsString(2).toString());
			        	Log.d("Tap "+(j+1)+" :::::::", link);
			        	Log.d("Tap "+(j+1)+" :::::::", ""+Tap);
						//Add du lieu
			        	ListFilmForMedia.lstTap.add(Tap+1);
			        	Log.d("list::::",""+ListFilmForMedia.lstTap.get(j));
			        	ListFilmForMedia.lstFilmUrl.add(link);
			        	Log.d("list::::",ListFilmForMedia.lstFilmUrl.get(j));
			        	
					}
		        }
		        
		      //Lay thong tin dien vien
		        request = (SoapObject) requestall.getProperty(0);
		        request2=(SoapObject) request.getProperty(0);
		        
		        //Log.d("Thong tin cac dien vien ::::","==============================");
		        SoapObject request3 = (SoapObject) request2.getProperty("Actors");
		        Log.d("dien vien ::::",request3.toString());
		        int SoDienVien = request3.getPropertyCount();
		        for (int j = 0; j < SoDienVien; j++) {
		        	request2=(SoapObject) request3.getProperty(j);
					arrActorID.add(request2.getProperty("ID").toString());
					arrActorName.add(request2.getProperty("Name").toString());
					arrActorPicture.add(request2.getProperty("ThumImage").toString());
					Log.d("dien vien ::::",arrActorName.get(j)+"--id::"+arrActorID.get(j)+"::::Link img : "+arrActorPicture.get(j));
					
				}
			}
			catch(Exception e){
				Log.d("Asynctask exception ::: ", e.toString());

			}
			return null;
		}
		
	}
}
